package com.vil.controller;

import com.vil.common.ServerResponse;
import com.vil.entity.Buildings;
import com.vil.entity.Detail;
import com.vil.entity.HouseProperty;
import com.vil.service.BuildingsService;
import com.vil.service.DetailService;
import com.vil.service.HousePropertyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/property")
public class HousePropertyController extends BaseController{

    @Autowired
    private HousePropertyService housePropertyService;
    
    @Autowired
    private BuildingsService buildingsService;

    /**
     * 房产管理
     * @param model
     * @return
     */
    //去房产页面
    @RequestMapping("propertyManagement.html")
    public String payService(Model model){
        if (this.isNull()==false){
            return "admin/login";
        }
        ServerResponse serverResponse = housePropertyService.getListByManyConditions(1, 5, null);
        model.addAttribute("houseProperty",serverResponse.getData());
        return "admin/homelist";
    }
    //获取房产列表
    @PostMapping("viewpropertylist")
    @ResponseBody
    public ServerResponse viewpropertylist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
    	HouseProperty houseProperty = new HouseProperty();
    	houseProperty.setProperty_name(condition);
        return housePropertyService.getListByManyConditions(pageIndex,pageSize,houseProperty);

    }

    //去新增页面
    @GetMapping("homeadd.html")
    public String homeadd(){
        return "admin/homeadd";
    }

    //新增房产
    @PostMapping("addHousePropertySelective")
    public ServerResponse addHousePropertySelective(HouseProperty houseProperty){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return housePropertyService.insertSelective(houseProperty);
    }

    //去编辑页面
    @GetMapping("home_edit.html/{id}")
    public String home_edit(@PathVariable Long id,Model model){
        ServerResponse byId = housePropertyService.getById(id);
        model.addAttribute("e",byId.getData());
        return "admin/home_edit";
    }

    //修改
    @PostMapping("editHome")
    @ResponseBody
    public ServerResponse editDeviceSelective(HouseProperty houseProperty){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return housePropertyService.updateByPrimaryKeySelective(houseProperty);
    }

    /**
     *栋数管理
     */

    //去栋数页面
    @RequestMapping("buildingManagement.html")
    public String buildingManagement(Model model){
        if (this.isNull()==false){
            return "admin/login";
        }
        ServerResponse serverResponse = buildingsService.getListByManyConditions(1, 5, null);
        model.addAttribute("buildings",serverResponse.getData());
        return "admin/buildinglist";
    }
    //获取栋数列表
    @PostMapping("viewbuildinglist")
    @ResponseBody
    public ServerResponse viewbuildinglist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                         @RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
                                         @RequestParam(value = "condition2",defaultValue = "",required = false) String condition2,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
        Buildings buildings = new Buildings();
        buildings.setVillage(condition);
        buildings.setBuilding_number(condition1);
        buildings.setBuilding_name(condition2);
        return buildingsService.getListByManyConditions(pageIndex,pageSize,buildings);

    }

    //去新增页面
    @GetMapping("buildingadd.html")
    public String buildingadd(){
        return "admin/buildingadd";
    }

    //新增设备
    @PostMapping("addBuildingsSelective")
    public ServerResponse addBuildingsSelective(Buildings buildings){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return buildingsService.insertSelective(buildings);
    }

    //去编辑页面
    @GetMapping("building_edit.html/{id}")
    public String building_edit(@PathVariable Long id,Model model){
        ServerResponse byId = buildingsService.getById(id);
        model.addAttribute("e",byId.getData());
        return "admin/building_edit";
    }

    //修改
    @PostMapping("editBuilding")
    @ResponseBody
    public ServerResponse editBuildingsSelective(Buildings buildings){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return buildingsService.updateByPrimaryKeySelective(buildings);
    }
}
